//
//  AnimationView.m
//  SizeManager
//
//  Created by 杨 敏 on 13-1-19.
//  Copyright (c) 2013年 杨 敏. All rights reserved.
//

#import "AnimationView.h"
#import "ConstantHeader.h"
#import "Tool.h"
@interface AnimationView()
{
    int _index;
    NSTimer *_timer;
}
@property(nonatomic,retain)NSArray *images;
@property(nonatomic,retain)UIImageView *imageView;

@end

@implementation AnimationView
-(id)initWithFrame:(CGRect)frame images:(NSArray *)images{
    self = [super initWithFrame:frame];
    if (self) {
        self.images=images;

        self.imageView =[[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:_imageView];
        
        [self reset];
    }
    return self;
}
-(void)removeFromSuperview
{
    if (_timer) {
       	[_timer invalidate];
        _timer=nil;
    }
    
	[super removeFromSuperview];
}
-(void)reset{
    if(_timer) {
       	[_timer invalidate];
        _timer=nil;
    }
    _index=0;
    UIImage *image = getSafeObjectAtIndex(_index, _images);
    _imageView.image=image;
}
-(void)play{
    UIImage *image=nil;
    
    if (checkList(_images)) {
        _index ++;
        _index = _index%_images.count;
        image = getSafeObjectAtIndex(_index, _images);
        
        if (_index+1==_images.count) {
            _index=0;
        }
    }
  
    _imageView.image=image;
}
-(void)fire{
    if (!_timer) {
        _timer =[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(play) userInfo:nil repeats:YES];
    }

    [_timer fire];
}
-(void)dealloc{

    _timer= nil;

}

@end
